#include <GA\operations\selection\RankSlector.hpp>

unitraf::RankSlector::RankSlector(void)
{
}


unitraf::RankSlector::~RankSlector(void)
{
}

size_t unitraf::RankSlector::select( )
{
	size_t size = _fit->size( );
	//calc n  = sum of all intervals. bigger index has bigger interval
	// n = ( size ) + ( size - 1 ) + ... + 3 + 2 + 1
	size_t n =  ( size - 1 ) * size / 2 + size;

	size_t rnd = static_cast<size_t>(rand( ) % n); // generates random number belonging to one of this intervals


	//decide witch interval rnd belong to
	size_t sum = size - 1;
	int i = 0;

	while ( sum < rnd )
	{
		++i;
		sum += size - i;
	} 
	
	// i - randomly generated rank.
	return i;
}
